using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using EastBancTech.InstallerDefinition;
using Microsoft.VisualStudio.Modeling;

namespace EBT.Deployment.SoftwareFactory.FileActions.Helpers
{
    public class NamedElement<TElement>
    {
        public NamedElement(string name, TElement element)
        {
            _name = name;
            _element = element;
        }

        private TElement _element;
        public TElement Element
        {
            get
            {
                return _element;
            }
            set
            {
                _element = value;
            }
        }

        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public override string ToString()
        {
            return _name;
        }
    }
    public abstract class FileActionHelper
    {
        public static FileActionHelper CreateInstance(DomainClassInfo classInfo)
        {
            Type type = Assembly.GetExecutingAssembly().GetType("EBT.Deployment.SoftwareFactory.FileActions.Helpers." + classInfo.ImplementationClass.FullName);
            return (FileActionHelper)System.Activator.CreateInstance(type); 
        }

        public abstract IEnumerable GetValues(InstallerPackageModel package);
        public abstract string GetElementName(ModelElement element);
        public abstract void AddValuesToFileAction(Store store, FileAction fileAction, ModelElement[] elements);
    }

    namespace EastBancTech.InstallerDefinition
    {
        class XPathReplacement : FileActionHelper
        {
            public override IEnumerable GetValues(InstallerPackageModel package)
            {
                return package.Definitions.XPaths;
            }

            public override string GetElementName(ModelElement element)
            {
                return ((XPathValue)element).Name;
            }

            public override void AddValuesToFileAction(Store store, FileAction fileAction, ModelElement[] elements)
            {
                global::EastBancTech.InstallerDefinition.XPathReplacement
                    xPathReplacement = (global::EastBancTech.InstallerDefinition.XPathReplacement)fileAction;
                foreach (XPathValue xPath in elements)
                {
                    xPathReplacement.XPaths.Add(xPath);
                }
            }

        }

        class TockenReplacement : FileActionHelper
        {
            public override IEnumerable GetValues(InstallerPackageModel package)
            {
                return package.Definitions.Tokens;
            }

            public override string GetElementName(ModelElement element)
            {
                return ((TokenValue)element).Name;
            }

            public override void AddValuesToFileAction(Store store, FileAction fileAction, ModelElement[] elements)
            {
                global::EastBancTech.InstallerDefinition.TockenReplacement
                    tockenReplacement = (global::EastBancTech.InstallerDefinition.TockenReplacement)fileAction;
                foreach (TokenValue tocken in elements)
                {
                    tockenReplacement.Tokens.Add(tocken);
                }

            }
        
        }
    }


}
